<?php
class PlayerMatchRecordDao {
    function getPMRsForMatch(& $matchRecord) {
        $pmrs = array ();
        $pd = new PlayerDao();
        $rs = executeQuery("select fk_player_id, delta_id, completions, touchdowns, interceptions, casualties, mvps, exps, injury, fk_skill_id from player_deltas where fk_match_record_id=$matchRecord->id");
        while ($row = mysql_fetch_row($rs)) {
            $player = $pd->loadPlayer($row[0]);
            if ($player) {
	            $pmr = new PlayerMatchRecord($player, $matchRecord);
	            $pmr->init($row);
	            $pmrs[$player->playerId] = $pmr;
            }
        }
        uasort($pmrs, "sortPmrs");
        return $pmrs;
    }
    
    function findPMRsForPlayer($player) {
        $pmrs = array ();
        $mrDao = new MatchRecordDao();
        $rs = executeQuery("select fk_match_record_id, delta_id, completions, touchdowns, interceptions, casualties, mvps, exps, injury, fk_skill_id from player_deltas where fk_player_id=$player->playerId");
        while ($row = mysql_fetch_row($rs)) {
        	if ($row[0]) {
        		$matchRecord = $mrDao->load($row[0]);
        	} else {
        	    $matchRecord = null;
        	}
            $pmr = new PlayerMatchRecord($player, $matchRecord);
            $pmr->init($row);
            $pmrs[$row[0]] = $pmr;
        }
        uasort($pmrs, "sortPmrs");
        return $pmrs;
    }
}

function sortPmrs($a, $b) {
    $rc = $a->matchRecord->id - $b->matchRecord->id;
    if ($rc != 0) {
        return $rc;
    }

    $rc = $a->player->rosterNum - $b->player->rosterNum;
    if ($rc != 0) {
        return $rc;
    }

    return $a->player->playerId - $b->player->playerId;
}
/*
 * Represents the changes to a player from a match
 */
class PlayerMatchRecord {
    var $player;
    var $matchRecord;
    var $id;
    var $comps;
    var $tds;
    var $ints;
    var $cas;
    var $mvps;
    var $exps = 0;
    var $injury;
    var $skill;

    function PlayerMatchRecord(& $player, & $matchRecord) {
        $this->id = -1;
        $this->player = $player;
        $this->matchRecord = $matchRecord;
        if ($player->isJourneyman()) {
            $this->injury = "Dead";
        }
    }

    /**
     * Read the record from the database
     */
    function load() {
        $pid = $this->player->playerId;
        $mrid = $this->matchRecord->id;
        $rs = executeQuery("select fk_player_id, delta_id, completions, touchdowns, interceptions, casualties, mvps, exps, injury, fk_skill_id from player_deltas where fk_player_id=$pid and fk_match_record_id=$mrid");
        if ($row = mysql_fetch_row($rs)) {
            $this->init($row);
            return true;
        }
        return false;
    }

    /**
     * Insert or Update the record into the database 
     */
    function save() {
        if ($this->hasSkill()) {
            $skillId = $this->skill->id;
        } else {
            $skillId = "NULL";
        }
        if ($this->id > 0) {
            $rs = executeQuery("update player_deltas set completions=$this->comps, touchdowns=$this->tds, interceptions=$this->ints, casualties=$this->cas, mvps=$this->mvps, exps=$this->exps, injury='$this->injury', fk_skill_id=$skillId where $this->id=delta_id");
        } else {
            $pid = $this->player->playerId;
            $mrid = $this->matchRecord->id;
            $rs = executeQuery("insert into player_deltas (fk_player_id, fk_match_record_id, completions, touchdowns, interceptions, casualties, mvps, exps, injury, fk_skill_id) values ($pid, $mrid, $this->comps, $this->tds, $this->ints, $this->cas, $this->mvps, $this->exps, '$this->injury', $skillId)");
            $rs = executeQuery("select delta_id from player_deltas where fk_player_id=$pid and fk_match_record_id=$mrid order by 1 desc");
            if ($row = mysql_fetch_row($rs)) {
                $this->id = $row[0];
            }
        }
        return $rs;
    }

    /**
     * Delete the record from the database
     */
    function delete() {
        $rs = executeQuery("delete from player_deltas where $this->id=delta_id");
    }

    /** Initialises the data from the load method */
    function init($row) {
        global $skills;
        $i = 1; // skip first value which is the player ID
        $this->id = $row[$i++];
        $this->comps = $row[$i++];
        $this->tds = $row[$i++];
        $this->ints = $row[$i++];
        $this->cas = $row[$i++];
        $this->mvps = $row[$i++];
        $this->exps = $row[$i++];
        $this->injury = $row[$i++];
        $skill = $row[$i++];
        if (strlen($skill) > 0) {
            $this->skill = $skills[$skill];
        } else {
            unset ($this->skill);
        }
    }

    function getInjuryTypes() {
        return array (
            0 => "",
            1 => "MNG",
            2 => "Niggle",
            3 => "-MA",
            4 => "-Av",
            5 => "-AG",
            6 => "-ST",
            7 => "Dead"
        );
    }

    function hasSkill() {
        return isset ($this->skill) and strlen($this->skill->name) > 0;
    }

    function getSPPs() {
        return $this->comps + $this->exps + (2 * $this->cas) + (2 * $this->ints) + (3 * $this->tds) + (5 * $this->mvps);
    }

}
?>
